<template>
  <div class="enterprise-list">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <h1 class="page-title">企业管理</h1>
        <p class="page-desc">管理平台所有注册企业信息</p>
      </div>
      <div class="header-right">
        <el-button type="primary" @click="handleAdd">
          <el-icon>
            <Plus />
          </el-icon>
          添加企业
        </el-button>
      </div>
    </div>

    <!-- 搜索筛选 -->
    <el-card class="search-card">
      <el-form :model="searchForm" inline>
        <el-form-item label="企业名称">
          <el-input v-model="searchForm.name" placeholder="请输入企业名称" clearable style="width: 200px" />
        </el-form-item>
        <el-form-item label="企业状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 120px">
            <el-option label="激活" value="active" />
            <el-option label="禁用" value="inactive" />
          </el-select>
        </el-form-item>
        <el-form-item label="行业">
          <el-input v-model="searchForm.industry" placeholder="请输入行业" clearable style="width: 150px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon>
              <Search />
            </el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon>
              <Refresh />
            </el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 企业列表 -->
    <el-card class="table-card">
      <el-table :data="tableData" v-loading="loading" stripe style="width: 100%">
        <el-table-column fixed prop="id" label="ID" width="80" />

        <el-table-column label="企业信息" min-width="200">
          <template #default="{ row }">
            <div class="enterprise-info">
              <el-avatar :size="40" :src="row.logoUrl">
                <el-icon>
                  <OfficeBuilding />
                </el-icon>
              </el-avatar>
              <div class="info-content">
                <div class="enterprise-name">{{ row.name }}</div>
                <div class="enterprise-short">{{ row.shortName }}</div>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="industry" label="行业" width="120" />

        <el-table-column prop="size" label="规模" width="100" />

        <el-table-column label="联系信息" min-width="180">
          <template #default="{ row }">
            <div class="contact-info">
              <div>{{ row.contactName }}</div>
              <div class="contact-email">{{ row.contactEmail }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="location" label="地址" width="120" show-overflow-tooltip />

        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 'active' ? 'success' : 'danger'">
              {{ row.status === 'active' ? '激活' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.createdAt) }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="300" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleView(row)">
              <el-icon>
                <View />
              </el-icon>
              查看
            </el-button>
            <el-button type="primary" link @click="handleEdit(row)">
              <el-icon>
                <Edit />
              </el-icon>
              编辑
            </el-button>
            <el-button :type="row.status === 'active' ? 'warning' : 'success'" link @click="handleToggleStatus(row)">
              <el-icon>
                <Switch />
              </el-icon>
              {{ row.status === 'active' ? '禁用' : '启用' }}
            </el-button>
            <el-button type="danger" link>
              <el-icon>
                <Remove />
              </el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination v-model:current-page="pagination.current" v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]" :total="pagination.total" layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </el-card>

    <CusElDialog v-model="dialogVisible" :is-full="true">
      <el-button type="primary">我是弹窗</el-button>
    </CusElDialog>
  </div>
</template>

<script setup lang="ts">
import CusElDialog from '@/components/cus-el-dialog/CusElDialog.vue'
import { onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Plus,
  Search,
  Refresh,
  OfficeBuilding,
  View,
  Edit,
  Switch
} from '@element-plus/icons-vue'
import usePageState from './hooks/usePageState'

const {
  dialogVisible,
  loading,
  searchForm,
  pagination,
  tableData,
  formatDate
} = usePageState();

const handleSearch = () => {
  pagination.current = 1
  loadData()
}

const handleReset = () => {
  Object.assign(searchForm, {
    name: '',
    status: '',
    industry: ''
  })
  handleSearch()
}

const handleAdd = () => {
  ElMessage.info('添加企业功能开发中')
  dialogVisible.value = true
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleView = (row: any) => {
  ElMessage.info(`查看企业：${row.name}`)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleEdit = (row: any) => {
  ElMessage.info(`编辑企业：${row.name}`)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleToggleStatus = (row: any) => {
  const action = row.status === 'active' ? '禁用' : '启用'
  ElMessageBox.confirm(
    `确定要${action}企业「${row.name}」吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    row.status = row.status === 'active' ? 'inactive' : 'active'
    ElMessage.success(`${action}成功`)
  })
}

const handleSizeChange = (size: number) => {
  pagination.size = size
  loadData()
}

const handleCurrentChange = (current: number) => {
  pagination.current = current
  loadData()
}

const loadData = () => {
  loading.value = true
  // TODO: 调用API获取数据
  setTimeout(() => {
    pagination.total = 2
    loading.value = false
  }, 500)
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.enterprise-list {
  padding: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 4px 0;
}

.page-desc {
  color: #909399;
  margin: 0;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  border-radius: 8px;
}

.enterprise-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.info-content {
  flex: 1;
}

.enterprise-name {
  font-weight: 600;
  color: #303133;
  margin-bottom: 2px;
}

.enterprise-short {
  font-size: 12px;
  color: #909399;
}

.contact-info {
  line-height: 1.5;
}

.contact-email {
  font-size: 12px;
  color: #909399;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    gap: 16px;
  }

  .header-right {
    align-self: stretch;
  }
}
</style>
